import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/set/JinShenShenHe.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/LoadingUtil.dart';
import 'package:panda/util/SignUtil.dart';

import 'ArticleDetail.dart';

class JinShenPage extends StatefulWidget {
  JinShenPage({Key key}) : super(key: key);

  @override
  _JinShenPageState createState() => _JinShenPageState();
}

class _JinShenPageState extends State<JinShenPage> {
  String token = "", string1 = "", string2 = "", string3 = "", group_id = "1";
  Asset img1, img2, img3;
  bool isEnable = true;
  String name = "";
  File file1, file2;
  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      getNext();
    });
  }

  getNext() async {
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = SignType.GET_NEXT_GROUP;
    Response<String> response =
        await HttpUtil().post(Api.GET_NEXT_GROUP, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    if (code == 1 && mounted) {
      name = userMap['data']['name'];
      group_id = userMap['data']['id'].toString();
      setState(() {});
    } else {
      Fluttertoast.showToast(msg: msg);
      isEnable = false;
      setState(() {});
    }
  }

  Future<void> selImg1(int type) async {
    // if (type == 1) {
    //   file1 = await FilePicker.getFile();
    //   print(file1 == null);

    //   if (file1 == null) {
    //     return;
    //   }
    //   string1 = file1.path;
    //   setState(() {});
    //   return;
    // }
    // if (type == 2) {
    //   file2 = await FilePicker.getFile();
    //   print(file2.path);
    //   if (file2 == null) {
    //     return;
    //   }
    //   string2 = file2.path;
    //   setState(() {});
    //   return;
    // }
    List<Asset> resultList;
    try {
      resultList = await MultiImagePicker.pickImages(
        maxImages: 1,
        enableCamera: true,
        materialOptions: MaterialOptions(
          actionBarTitle: "选择图片",
          allViewTitle: "已选择",
          actionBarColor: "#F1120D",
          actionBarTitleColor: "#ffffff",
          lightStatusBar: false,
          statusBarColor: "#F1120D",
          startInAllView: true,
          selectCircleStrokeColor: "#F1120D",
          selectionLimitReachedText: "已超出",
        ),
      );
    } on Exception catch (e) {
//      error = e.message;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
    if (resultList == null || resultList.length <= 0) {
      return;
    }
    if (type == 1) {
      img1 = resultList[0];
    } else if (type == 2) {
      img2 = resultList[0];
    } else if (type == 3) {
      img3 = resultList[0];
      string3 = img3.name;
    }

    // upFilePath = await resultList[0].filePath;
    // upHead(resultList[0]);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(
          '晋升申请',
          style: TextStyle(
              color: black,
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
        ),
        elevation: 0.2,
        backgroundColor: white,
        iconTheme: new IconThemeData(color: black_99),
        brightness: Brightness.light,
       ),
      body: Container(
          width: double.infinity,
          child: Column(
            children: <Widget>[
              Container(
                padding:
                    EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
                margin: EdgeInsets.fromLTRB(
                    0, ScreenUtil.getInstance().setHeight(DP_15), 0, 0),
                color: Colors.white,
                child: new GestureDetector(
                  onTap: () async {
                    if (!isEnable) {
                      return;
                    }
                    var result = await showDialog(
                        context: context,
                        builder: (context) {
                          return SimpleDialog(
                            title: Text('选择申请级别'),
                            children: <Widget>[
                              SimpleDialogOption(
                                child: Text('经理'),
                                onPressed: () {
                                  name = "经理";
                                  Navigator.pop(context, '3');
                                },
                              ),
                              Divider(),
                              SimpleDialogOption(
                                child: Text('总监'),
                                onPressed: () {
                                  name = "总监";
                                  Navigator.pop(context, '4');
                                },
                              ),
                              Divider(),
                            ],
                          );
                        });

                    // print(result);
                    if (result == null) {
                      return;
                    }
                    group_id = result;
                    setState(() {});
                  },
                  behavior: HitTestBehavior.opaque,
                  child: new Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          '晋升级别',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE),
                              color: Colors.black87),
                        ),
                      ),
                      Text(name,
                          style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                      Icon(Icons.arrow_drop_down)
                    ],
                  ),
                ),
              ),
              // Container(
              //   padding:
              //       EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
              //   margin: EdgeInsets.fromLTRB(
              //       0, ScreenUtil.getInstance().setHeight(2), 0, 0),
              //   color: Colors.white,
              //   child: new GestureDetector(
              //     onTap: () {
              //       if (Platform.isIOS) {
              //         Fluttertoast.showToast(msg: "苹果不支持此操作,请尝试下方直接申请");
              //         return;
              //       }
              //       selImg1(1);
              //     },
              //     behavior: HitTestBehavior.opaque,
              //     child: new Row(
              //       children: <Widget>[
              //         Text(
              //           '申请表',
              //           style: TextStyle(
              //               fontSize: ScreenUtil.getInstance()
              //                   .setSp(NORMAL_FONT_SIZE),
              //               color: Colors.black87),
              //         ),
              //         Expanded(
              //           child: Text(string1 == "" ? "暂未上传" : string1,
              //               textAlign: TextAlign.end,
              //               style: TextStyle(
              //                   color: Colors.black54,
              //                   fontSize: ScreenUtil.getInstance()
              //                       .setSp(NORMAL_FONT_SIZE))),
              //         ),
              //         Icon(
              //           Icons.add_box,
              //           color: Colors.grey,
              //           size: 20,
              //         )
              //       ],
              //     ),
              //   ),
              // ),
              // Container(
              //   padding:
              //       EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
              //   margin: EdgeInsets.fromLTRB(
              //       0, ScreenUtil.getInstance().setHeight(2), 0, 0),
              //   color: Colors.white,
              //   child: new GestureDetector(
              //     onTap: () {
              //       if (Platform.isIOS) {
              //         Fluttertoast.showToast(msg: "苹果不支持此操作,请尝试下方直接申请");
              //         return;
              //       }
              //       selImg1(2);
              //     },
              //     behavior: HitTestBehavior.opaque,
              //     child: new Row(
              //       children: <Widget>[
              //         Text(
              //           '述职报告',
              //           style: TextStyle(
              //               fontSize: ScreenUtil.getInstance()
              //                   .setSp(NORMAL_FONT_SIZE),
              //               color: Colors.black87),
              //         ),
              //         Expanded(
              //             child: Text(string2 == "" ? "暂未上传" : string2,
              //                 textAlign: TextAlign.end,
              //                 style: TextStyle(
              //                     color: Colors.black54,
              //                     fontSize: ScreenUtil.getInstance()
              //                         .setSp(NORMAL_FONT_SIZE)))),
              //         Icon(
              //           Icons.add_box,
              //           color: Colors.grey,
              //           size: 20,
              //         )
              //       ],
              //     ),
              //   ),
              // ),
              // Container(
              //   padding:
              //       EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
              //   margin: EdgeInsets.fromLTRB(
              //       0, ScreenUtil.getInstance().setHeight(2), 0, 0),
              //   color: Colors.white,
              //   child: new GestureDetector(
              //     onTap: () {
              //       selImg1(3);
              //     },
              //     behavior: HitTestBehavior.opaque,
              //     child: new Row(
              //       children: <Widget>[
              //         Text(
              //           '营业执照',
              //           style: TextStyle(
              //               fontSize: ScreenUtil.getInstance()
              //                   .setSp(NORMAL_FONT_SIZE),
              //               color: Colors.black87),
              //         ),
              //         Expanded(
              //           child: Text(img3 == null ? "暂未上传" : string3,
              //               textAlign: TextAlign.end,
              //               style: TextStyle(
              //                   color: Colors.black54,
              //                   fontSize: ScreenUtil.getInstance()
              //                       .setSp(NORMAL_FONT_SIZE))),
              //         ),
              //         Icon(
              //           Icons.add_box,
              //           color: Colors.grey,
              //           size: 20,
              //         )
              //       ],
              //     ),
              //   ),
              // ),
              // 
              Container(height: 10),
              GestureDetector(
                child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      '申请则视为同意',
                      style: TextStyle(
                          color: black,
                          fontSize: ScreenUtil.getInstance().setSp(25)),
                    ),
                    Text(
                      '《熊猫新零售晋升制度》',
                      style: TextStyle(
                          color: mainColor,
                          fontSize: ScreenUtil.getInstance().setSp(28)),
                    ),
                  ],
                ),
                onTap: () {
                  Navigator.of(context)
                      .push(MaterialPageRoute(builder: (context) {
                    return new ArticleDetailPage(
                      article_id: "52",
                    );
                  }));
                },
              ),
              new Container(
                width: double.infinity,
                height: ScreenUtil.getInstance().setHeight(DP_80),
                margin: EdgeInsets.fromLTRB(30, 80, 30, 0),
                decoration: new BoxDecoration(
                    color: isEnable ? mainColor : Colors.black45,
                    borderRadius: BorderRadius.circular(
                        ScreenUtil.getInstance().setHeight(DP_10))),
                padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                child: new FlatButton(
                  child: new Padding(
                    padding: EdgeInsets.all(0.0),
                    child: new Text(
                      isEnable ? "确定提交" : "当前已最高级别",
                      style: new TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  ),
                  onPressed: () {
                    if (isEnable) {
                      showDialog(
                          context: context,
                          barrierDismissible: false,
                          builder: (_) {
                            return new NetLoadingDialog(
                              loadingText: "提交中...",
                              bgColor: Color.fromARGB(255, 0, 0, 0),
                              requestCallBack: null,
                              outsideDismiss: false,
                            );
                          });
                      up1();
                    }
                  },
                ),
              ),
              // Expanded(
              //   child: Center(),
              // ),
              // new Container(
              //   width: double.infinity,
              //   height: ScreenUtil.getInstance().setHeight(70),
              //   margin: EdgeInsets.fromLTRB(60, 80, 60, 30),
              //   decoration: new BoxDecoration(
              //       color: isEnable ? mainColor : Colors.black45,
              //       borderRadius: BorderRadius.circular(
              //           ScreenUtil.getInstance().setHeight(DP_10))),
              //   padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              //   child: new FlatButton(
              //     child: new Padding(
              //       padding: EdgeInsets.all(0.0),
              //       child: new Text(
              //         "直接申请",
              //         style: new TextStyle(color: Colors.white, fontSize: 16),
              //       ),
              //     ),
              //     onPressed: () async {
              //       if (isEnable) {
              //         var result = await showDialog(
              //             context: context,
              //             builder: (context) {
              //               return SimpleDialog(
              //                 title: Text('选择申请级别'),
              //                 children: <Widget>[
              //                   SimpleDialogOption(
              //                     child: Text('经理'),
              //                     onPressed: () {
              //                       Navigator.pop(context, '3');
              //                     },
              //                   ),
              //                   Divider(),
              //                   SimpleDialogOption(
              //                     child: Text('总监'),
              //                     onPressed: () {
              //                       Navigator.pop(context, '4');
              //                     },
              //                   ),
              //                   Divider(),
              //                 ],
              //               );
              //             });

              //         // print(result);
              //         if (result == null) {
              //           return;
              //         }
              //         showDialog(
              //             context: context,
              //             barrierDismissible: false,
              //             builder: (_) {
              //               return new NetLoadingDialog(
              //                 loadingText: "提交中...",
              //                 bgColor: Color.fromARGB(255, 0, 0, 0),
              //                 requestCallBack: null,
              //                 outsideDismiss: false,
              //               );
              //             });
              //         up1(result);
              //       }
              //     },
              //   ),
              // ),
              // Container(
              //   color: white,
              //   padding: EdgeInsets.all(20),
              //   child: Row(
              //     mainAxisAlignment: MainAxisAlignment.center,
              //     children: <Widget>[
              //       Icon(
              //         Icons.file_download,
              //         color: mainColor,
              //         size: DP_30,
              //       ),
              //       Container(
              //         width: 10,
              //       ),
              //       Text('资料下载',
              //           style: TextStyle(
              //               color: mainColor,
              //               fontSize:
              //                   ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)))
              //     ],
              //   ),
              // )
            ],
          )),
    );
  }
  up1() async {
    Map map = new Map();
    map['token'] = token;
    map['group_id'] = group_id;
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + "/index.php/wap/UserUpgradeApply/apply",
        data: getSign(map));
    Map userMap = json.decode(response.data);
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: userMap['msg']);
    if (userMap['code'] == 1 && mounted) {
      Navigator.of(context).pop("1");
      // APPLIC.userInfoMap=userMap['data'];
      // APPLIC.eventBus.fire(UserInfoEvent(APPLIC.userInfoMap));
    }
  }
}
